<!doctype html>
<html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>json格式化</title>
	<link rel="stylesheet" href="dist/jquery.jsonview.css" />
	<style>
		html,body{
			height: 100%;
			width: 100%;
			background-color: #f5f5f5;
				background: linear-gradient(90deg, #649173 10%, #DBD5A4 90%);
		}
		#jsonformat{
			width: 50% ;
			height: 100% ;
			height: 300px;
			vertical-align: top;
		}
		#jsonview{
			display: inline-block;
		}
		button{
			display: inline-block;
			width: 100px;
			height: 30px;
			background-color: lightblue;
			color: white;
			border-radius: 10px;
		}
		textarea{
			background-color: rgba(0, 0, 0, 0.5);
			color: white;
			border-radius: 15px;
		}
	</style>
</head>
<body >
	<button  onclick="fn_formatJson()">格式化</button>
	<button  onclick="getData()">获取数据</button>
	<button  onclick="setData()">设置数据</button>
	<br />
	<textarea  id="jsonformat" >
		
	</textarea>
	
	<div id="jsonview" ></div>
	<!--<script type="text/javascript" src="js/jquery-1.11.0.min.js" ></script>-->
	<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" src="dist/jquery.jsonview.js" ></script>
	<script type="text/javascript">
		
		function fn_formatJson(){
			try{
				var data = $("#jsonformat").val()
				data = JSON.parse(data)
				data = JSON.stringify(data, null, 4)
				$("#jsonformat").val(data)
				$("#jsonview").JSONView(data)
			}
			catch(e){
				alert("json数据出错，请检查！\n"+e.message)
				console.log(e)
				}
		}
		
		$("#jsonformat").bind('paste',function(e){
//			fn_formatJson()
			console.log(e)
		})
		function getData(){
			$.ajax({
				type:"get",
				url:"http://zrb.gotohard.cn/config/spider",
				async:true,
				success:function(data){
					$("#jsonformat").val(JSON.stringify(data))
					fn_formatJson()
					var jsonViewH = $("#jsonview").height();
					console.log(jsonViewH)
					$("textarea").css({
						"height":jsonViewH+"px"
					})
				}
			});
		}
		function setData(){
			postdata = $("#jsonformat").val()
			$.ajax({
				type:"post",
				data:postdata,
				url:"http://zrb.gotohard.cn/config/wconfig",
				async:true,
				success:function(data){
					alert("设置请求已经提交")
					console.log(data)
				}
			});
		}
	</script>
</body>
</html>